package com.ddd.mill.query.product;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddd.mill.domain.aggregate.product.entity.Product;
import com.ddd.mill.domain.aggregate.product.entity.ProductDetail;
import com.ddd.mill.domain.aggregate.product.entity.ProductImg;
import com.ddd.mill.domain.aggregate.producttype.entity.ProductType;
import com.ddd.mill.domain.aggregate.sku.entity.ProductSku;
import com.ddd.mill.domain.aggregate.sku.entity.ProductSkuValue;
import com.ddd.mill.api.product.dto.request.GetProductPageParamDto;
import com.ddd.mill.api.product.dto.response.GetProductPageDto;
import com.ddd.mill.api.product.dto.response.ProductDetailDto;
import com.ddd.mill.domain.aggregate.attributevalue.entity.AttributeValue;
import com.ddd.mill.infrastructure.exception.BizException;
import com.ddd.mill.infrastructure.persistent.dao.attributevalue.AttributeValueDao;
import com.ddd.mill.infrastructure.persistent.dao.product.*;
import com.ddd.mill.infrastructure.utils.TypeConvertUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class ProductQueryServiceMyBatisServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductQueryService {
    @Resource
    ProductDetailDao productDetailDao;
    @Resource
    ProductImgDao productImgDao;
    @Resource
    ProductSkuDao productSkuDao;
    @Resource
    ProductSkuValueDao productSkuValueDao;
    @Resource
    ProductTypeDao productTypeDao;
    @Resource
    AttributeValueDao attributeValueDao;
    @Override
    public IPage<GetProductPageDto> productList(GetProductPageParamDto dto, Page page) {
        page.setOrders(OrderItem.descs("p.create_time"));
        return baseMapper.getProductPage(page,dto);
    }
    @Override
    public ProductDetailDto productDetail(Long id) {
        Product product = this.getById(id);
        if (product == null){
            throw new BizException("商品不存在");
        }// 基本信息
        ProductDetailDto rtn = TypeConvertUtils.convert(product, ProductDetailDto.class);
        // 详情信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", id);
        ProductDetail productDetail = productDetailDao.selectOne(queryWrapper);
        rtn.setDetail(productDetail.getDetail());
        rtn.setDeliverTips(productDetail.getDeliverTips());
        // 轮播图
        List<ProductImg> productImgList = productImgDao.selectList(queryWrapper);
        rtn.setImgList(new ArrayList<>());
        for (ProductImg p : productImgList){
            rtn.getImgList().add(p.getImgUrl());
        }
        // 库存
        // 整理sku
        List<ProductSku> productSkuList = productSkuDao.selectList(queryWrapper);
        rtn.setSkuList(new ArrayList<>());
        if (!productSkuList.isEmpty()) {
            Map<Long, JSONObject> productSkuMap = new HashMap<>();
            Set<Long> productSkuIdSet = new HashSet<>();
            for (ProductSku p : productSkuList){
                JSONObject json = new JSONObject();
                rtn.getSkuList().add(json);
                productSkuMap.put(p.getId(), json);
                json.put("id", p.getId());
                json.put("price", p.getPrice());
                json.put("groupMonthPrice", p.getGroupMonthPrice());
                json.put("groupNowPrice", p.getGroupNowPrice());
                json.put("flatbedPrice", p.getFlatbedPrice());
                json.put("dividePrice", p.getDividePrice());
                json.put("stockNumber", p.getStockNumber());
                json.put("remark", p.getStockNumber());
                productSkuIdSet.add(p.getId());
            }
            queryWrapper = new QueryWrapper();
            queryWrapper.in("sku_id", productSkuIdSet);
            List<ProductSkuValue> productSkuValueList = productSkuValueDao.selectList(queryWrapper);
            Set<Long> attributeIdSet = new HashSet<>();
            Set<Long> attributeValueIdSet = new HashSet<>();

            ProductType productType = productTypeDao.selectById(product.getProductTypeId());
            queryWrapper = new QueryWrapper();
            queryWrapper.in("attribute_id", productType.getAttributeIds().split(","));
            Map<Long, AttributeValue> attributeValueMap = TypeConvertUtils.convert(attributeValueDao.selectList(queryWrapper), "id");

            for (ProductSkuValue p : productSkuValueList){
                JSONObject json = productSkuMap.get(p.getSkuId());
                if (json != null){
                    AttributeValue attributeValue = attributeValueMap.get(p.getAttributeValueId());
                    json.put(p.getAttributeId().toString(), attributeValue == null ? "" : attributeValue.getVal());
                }
                attributeIdSet.add(p.getAttributeId());
                attributeValueIdSet.add(p.getAttributeValueId());
            }
        }
        return rtn;
    }
}
